//
//  XYBookDetailsHeaderView.swift
//  XYReadBook
//
//  Created by fox on 2017/9/7.
//  Copyright © 2017年 tsaievan. All rights reserved.
//
// 展示逻辑
/*
 我是至尊
        作者:风凌天下
 图片    类型:玄幻
        字数:3000万字
        状态:连载中
        更新:2个小时前
 
 text正文  点击展开
 最新目录   跳转目录
 
 推荐相同喜好书籍
 九宫格样式展示
 
 VC
 加入书架  点击阅读
 */
import UIKit
protocol XYBookDetailsHeaderViewDelegate:NSObjectProtocol {
    //设置协议方法
    func bookDetailsUpdateFrame(HeaderView:XYBookDetailsHeaderView)
    
    /// 跳转目录
    func bookDetailsGotoList(HeaderView:XYBookDetailsHeaderView)
    
    /// 点击阅读
    func bookDetailsReadBook(HeaderView:XYBookDetailsHeaderView)
    
    
    /// 直接跳转最后一章
    ///
    /// - Parameter HeaderView: 直接条跳转最后一个章节 
    func bookDetailsGoToLastChapter(HeaderView:XYBookDetailsHeaderView)
}
/*
 lab 之间 间距上下为0
        我是至尊
        作者:风凌天下
 图片    类型:玄幻
        字数:3000万字
        状态:连载中
        更新:2个小时前
 
 text正文  点击展开
 最新目录   跳转目录
 
 推荐相同喜好书籍
 九宫格样式展示
 
 VC
 点击阅读
 */
class XYBookDetailsHeaderView: UICollectionReusableView {
    
    var model:XYBookDetailModel? {
        didSet {
            if let model = model {
                let imageUrl = BaseUrl.image.rawValue.appending(model.cover)
                iconImage.setImageView(urlString: imageUrl, placeHolder:"Image_loading")
                blurryImage.setImageView(urlString: imageUrl, placeHolder: "Image_loading")
                
                bookNameLab.text = model.title
               authorLab.text = String.init(format: "作者: %@", model.author)
               typeBookLab.text = String.init(format: "类型: %@ %@", model.majorCate,model.minorCate)
                workCountLab.text = String.init(format: "字数: %zd万字", model.wordCount/10000)
                let timeStr = model.updated
                let time = (timeStr as NSString).substring(to: 19)
                let dateString  = XYPastTimeFormatter.sinaTimeStrToDate(sinaTimeStr: time)
                let str  = XYPastTimeFormatter.shared.string(for: dateString)
                if let str  = str{
                    updateTimeLab.text = String.init(format: "更新: %@", str)
                }
                
                
                bookTextLab.text = String.init(format: "内容简介: %@", model.longIntro)
                chapterlistBtn.setTitle(model.lastChapter, for: .normal)
            }
        }
    }
    
    
    var typeStr:String = "" {
        didSet {
            var str = ""
            switch typeStr {
            case "hot":
                str = "热门"
                break
            case "reputation":
                 str = "热门"
                break
            case "new":
                str = "新书"
                break
            case "over":
                str = "完结"
                break
            default: break
            }
             stateLab.text = String.init(format: "类型: %@", str)
        }
    }
    
    weak var delegate:XYBookDetailsHeaderViewDelegate?
    var blurryImage = UIImageView()
    let vBlur = UIVisualEffectView(effect:UIBlurEffect(style:.light))
    
    var iconImage      = UIImageView() //封面 80 * 180
    var bookNameLab    = UILabel()// 书籍名字 font 20
    var authorLab      = UILabel()// 作者 // 14
    var typeBookLab    = UILabel()// 类型
    var workCountLab   = UILabel()// 字数
    var stateLab       = UILabel()// 状态
    var updateTimeLab  = UILabel()// 更新时间
    var bookTextLab    = UILabel()// 内容
    
    var chapterlistBtn = UIButton()// 目录
    var readImageBtn   = UIButton()//阅读按钮 头像
   
    var bookView       = UIView() /// 管理书籍封面信息
    
    
    var infoChapterBtn = UIButton() ///查看目录
    var chapterView    = UIView() // 管理:查看目录
    
    var buttomLab      = UILabel()
    var buttomView     = UIView() ///底部一个 相同喜好 推荐
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    
    func setupUI() {
        bookView.insertSubview(blurryImage, at: 0)
        blurryImage.contentMode = .scaleAspectFill
        blurryImage.clipsToBounds = true
        vBlur.alpha = 1
        blurryImage.alpha = 0.2
        blurryImage.addSubview(vBlur)
       
        buttomView.backgroundColor = UIColor.hm_color(withRed: 242, green: 242, blue: 242)

        
        self.addSubview(bookView)
       
        self.addSubview(chapterView)
        self.addSubview(buttomView)
        
        
        chapterView.isUserInteractionEnabled = true
        
        bookView.addSubview(readImageBtn)
        bookView.addSubview(iconImage)
        bookView.addSubview(bookNameLab)
        bookView.addSubview(authorLab)
        bookView.addSubview(typeBookLab)
        bookView.addSubview(workCountLab)
        bookView.addSubview(stateLab)
        bookView.addSubview(updateTimeLab)
        
        /// 添加一个手势给内容正文
        self.addSubview(bookTextLab)
        bookTextLab.textColor = UIColor.hm_color(withRed: 100, green: 100, blue: 100)
        bookTextLab.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(changeFrame))
        bookTextLab.addGestureRecognizer(tap)
        
        chapterView.addSubview(infoChapterBtn)
        chapterView.addSubview(chapterlistBtn)
        
        infoChapterBtn.addTarget(self, action: #selector(clickChapterList), for: .touchUpInside)

        chapterlistBtn.addTarget(self, action: #selector(goTolastChapter), for: .touchUpInside)
        
        buttomView.addSubview(buttomLab)
        
        
        bookNameLab.font = UIFont.boldSystemFont(ofSize: 19)
        authorLab.font = UIFont.systemFont(ofSize: 14)
        typeBookLab.font = UIFont.systemFont(ofSize: 14)
        workCountLab.font = UIFont.systemFont(ofSize: 14)
        stateLab.font = UIFont.systemFont(ofSize: 14)
        updateTimeLab.font = UIFont.systemFont(ofSize: 14)
        bookTextLab.font = UIFont.systemFont(ofSize: 15)
        bookTextLab.numberOfLines = 0
        chapterlistBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        chapterlistBtn.setTitleColor(UIColor.black, for: .normal)
         readImageBtn.setImage(UIImage.init(named: "readBtn"), for: .normal)
        
        
        infoChapterBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
   
        infoChapterBtn.setTitleColor(UIColor.hm_color(withRed: 108, green: 197, blue: 204), for: .normal)
       
        buttomLab.font = UIFont.systemFont(ofSize: 18)
       
        infoChapterBtn.setTitle("查看目录", for: .normal)
        buttomLab.text = "为您推荐"
        buttomLab.textColor = UIColor.black

        
       
        readImageBtn.addTarget(self, action: #selector(clickRead), for: .touchUpInside)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        bookView.frame = CGRect.init(x:0, y: 0, width: ScreenWidth, height: 148)
        vBlur.frame = bookView.frame
        blurryImage.frame = bookView.frame
        readImageBtn.frame = CGRect.init(x: ScreenWidth - 54, y: bookView.centerY - 12, width: 36.5, height: 20)
         readImageBtn.extendEdgeInsets = UIEdgeInsetsMake(70, 70, 70, 10) //扩大范围
        
        iconImage.frame = CGRect.init(x: 12, y: 14, width: 80, height: 120)
        bookNameLab.frame = CGRect.init(x: iconImage.frame.maxX + 12, y: 18, width: ScreenWidth / 2, height: 20)
        authorLab.frame = CGRect.init(x: bookNameLab.x, y: bookNameLab.frame.maxY, width: bookNameLab.width, height: bookNameLab.height)
        typeBookLab.frame = CGRect.init(x: authorLab.x, y: authorLab.frame.maxY, width: authorLab.width, height: bookNameLab.height)
        workCountLab.frame = CGRect.init(x: typeBookLab.x, y: typeBookLab.frame.maxY, width: typeBookLab.width, height: bookNameLab.height)
        stateLab.frame = CGRect.init(x: workCountLab.x, y: workCountLab.frame.maxY, width: workCountLab.width, height: bookNameLab.height)
        updateTimeLab.frame = CGRect.init(x: stateLab.x, y: stateLab.frame.maxY, width: stateLab.width, height: bookNameLab.height)
        
        var textH = 90
        if self.height > 340 {
            textH += Int(self.height) - 340
        }
        
       
        bookTextLab.frame = CGRect.init(x: 12, y: bookView.frame.maxY + 6, width: ScreenWidth - 24, height: CGFloat(textH))
        
        chapterView.frame = CGRect.init(x: 0, y: bookTextLab.frame.maxY + 6, width: ScreenWidth, height: 44)
        infoChapterBtn.frame = CGRect.init(x: 12, y: 12, width: 60, height: bookNameLab.height)
        chapterlistBtn.frame = CGRect.init(x: infoChapterBtn.frame.maxX, y: 0, width: ScreenWidth - 90, height: 44)
        
        buttomView.frame = CGRect.init(x: 0, y: chapterView.frame.maxY + 6, width: ScreenWidth, height: 44)
        buttomLab.frame = CGRect.init(x: 12, y: 12, width: 100, height: bookNameLab.height)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    /// 跳转最后一章节
    func goTolastChapter () {
        delegate?.bookDetailsGoToLastChapter(HeaderView: self)
    }
    
    
    /// 点击阅读
    func clickRead() {
       delegate?.bookDetailsReadBook(HeaderView: self)
        
    }
    
    
    /// 点击跳转章节列表
    func clickChapterList() {
        delegate?.bookDetailsGotoList(HeaderView: self)
    }
    
    
    /// 更改Frame
    func changeFrame() {
    delegate?.bookDetailsUpdateFrame(HeaderView: self)
    }
}

